<?php
namespace app\seller\controller;
use think\Validate;
class Index
{
	public function __construct(){
		seller_auth();
	}
	
    public function index()
    {
		$uid = session('wmo2o_u_uid');
		if(request()->isPost()){
			$data = input("post.");
			unset($data["edui-map-dynamic"]);
			$data['status'] = 3;
			db("user")->where(['type'=>1,'id' => $uid])->update($data);
		}
		$data = db("user")->where(['type'=>1,'id'=>$uid])->find();
        return view('',['data'  => $data]);
    }
	
	public function sellerinfo()
    {
		$uid = session('wmo2o_u_uid');
		if(request()->isPost()){
			$post = input("post.");
			$validate = new Validate([
				['headimg','require',lang('headimg').lang('required')],
				['name','require',lang('name').lang('required')],
			]);
			if (!$validate->check($post)) {
				return error($validate->getError());
			}
			$data = array(
				'pid' => $post['pid'],
				'cid' => $post['cid'],
				'aid' => $post['aid'],
				'name' => $post['name'],
				'headimg' => $post['headimg'],
			);
			db("user")->where(['type'=>1,'id' => $uid])->update($data);
			//db("service")->where(['uid' => $uid])->update(array('pid' => $post['pid'],'cid' => $post['cid'],'aid' => $post['aid']));
			
		}
		$data = db("user")->where(['type'=>1,'id'=>$uid])->find();
        return view('',['data'  => $data]);
    }
	
	 public function service()
    {
		$uid = session('wmo2o_u_uid');
		if(!db("user")->where(['type'=>1,'id'=>$uid,'status'=>1])->find()){
			return error(lang('seller_error'));
			exit;
		}
		$data = db("service")->where('uid',$uid)->select();
        return view('',['data'  => $data]);
    }
	
	public function service_edit()
    {
		$id = input('get.id');
		$uid = session('wmo2o_u_uid');
		if(!db("user")->where(['type'=>1,'id'=>$uid,'status'=>1])->find()){
			return error(lang('seller_error'));
			exit;
		}
		$data = array();
		if(request()->isPost()){
			$data = input("post.");
			$data['uid'] = $uid;
			$validate = new Validate([
				['title','require',lang('title').lang('required')],
				['price','require',lang('price').lang('required')],
				['service','require',lang('service').lang('required')],
				['content','require',lang('content').lang('required')],
			]);
			if (!$validate->check($data)) {
				return error($validate->getError());
			}
			$seller = db("user")->where(['type'=>1,'id' => $data['uid']])->find();
			$data['pid'] = $seller['pid'];
			$data['cid'] = $seller['cid'];
			$data['aid'] = $seller['aid'];
			$data['status'] = 3;
			if($id){
				db("service")->where(['id' => $id,'uid' => $data['uid']])->update($data);
				$data['id'] = $id;
			}else{
				$data['time'] = time();
				$data['id'] = db("service")->insertGetId($data);
			}
			redirect(url('/seller/index/service'));
			exit;
		}else{
			$data = db("service")->where(['id' => $id,'uid' => $uid])->find();
		}
        return view('',['data'  => $data]);
    }
	
	public function service_del()
    {
		$id = input("get.id");
		$uid = session('wmo2o_u_uid');
		if(!db("user")->where(['type'=>1,'id'=>$uid,'status'=>1])->find()){
			return error(lang('seller_error'));
			exit;
		}
		if(db("service")->where('uid',$uid)->delete($id)){
			return success();
			exit;
		}else{
			return error();
			exit;
		}
    }
	
	public function myservicead()
    {
		$data = db("notice")->where(['id'=>4])->find();
		return view('',['data'  => $data]);
    }
	
	public function preview(){
		$type = input('get.type');
		$id = input('get.id');
		$uid = session('wmo2o_u_uid');
		switch($type){
			case 'seller_detail':
				$data = db("user")->where(['id' => $uid,'type' => 1])->find();
				break;
			case 'goods_list':
				$data = db("service")->where(['uid' => $uid,'status' => 1])->select();
				break;
			case 'goods_detail':
				$data = db("service")->where(['id' => $id])->find();
				break;
		}
		
		return view($type,['data' => $data]);
	}
	
	public function ajax_get()
    {
		$gid = input('get.id')?input('get.id'):0;
		$data = db("service_category")->where(['gid'=>$gid])->order('id asc')->select();
		$return = array(
			'data' => $data
		);
		return $return;
    }
	
	public function ajax_get_city()
    {
		$type = input('get.type');
		$father_id = input('get.id');
		if(!empty($father_id)){
			$where = ['father_id'=>$father_id,'status'=>1];
		}else{
			$where = ['status'=>1];
		}
		$data = db($type)->where($where)->order('id asc')->select();
		$return = array(
			'data' => $data
		);
		return $return;
    }
	
	public function ajax_mod_password()
    {
		$uid = session('wmo2o_u_uid');
		$old = input('get.old');
		$new = input('get.new');
		if(db('user')->where(['id' => $uid,'password' => md5($old),'type'=>1])->find()){
			if(db('user')->where(['id' => $uid])->update(['password' => md5($new)])){
				$return = array(
					'code' => 'success',
					'msg' => lang('success')
				);
			}else{
				$return = array(
					'code' => 'error',
					'msg' => lang('error')
				);
			}
		}else{
			$return = array(
				'code' => 'error',
				'msg' => lang('error')
			);
		}
		return $return;
    }
}
